package util;

import sun.net.dns.ResolverConfiguration;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;


/**
 * Created by chenkuo on 2017/11/17.
 */
public class ConfigSql {
    private static Properties properties = null;
    private static Map<String, String> sqlmap = new ConcurrentHashMap();
    private final static String file = "mysql.properties";

    public static Map<String, String> config() {
        try {
            if(null == properties){
                synchronized (ConfigSql.class){
                    if(null == properties){
                        properties = new Properties();
                        properties.load(ConfigLoader.class.getClassLoader().getResourceAsStream(file));
                        Set<Map.Entry<Object, Object>> set = properties.entrySet();
                        Iterator<Map.Entry<Object,Object>> it =  set.iterator();
                        while(it.hasNext()){
                            Map.Entry<Object,Object> entry = it.next();
                            sqlmap.put(entry.getKey().toString(), entry.getValue().toString());

                        }
                    }
                }
            }

            if(sqlmap.size() <= 0){
                throw new RuntimeException("sql config init failed ...");
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return sqlmap;
    }


    public static String get(String id, Object ... params) {

           return String.format(config().get(id), params);
    }


    public static void main(String[] args) {
        System.out.println("sql is = " + ConfigSql.get("queryOrderId", "18770043965"));
    }


}
